import axios from 'axios'
import {API, ERR_CODE, API_TIMESTAMP} from './config'
import { ENV } from 'common/js/process'

export const PlayListAsync = `${API}/user/playlist` //  用户歌单

export const UserInfoAsync = `${API}/user/detail`
/**
 * 获取用户详情
 * @param uid {number || string} 用户 id
 */
export function GetUserInfo (uid) {
  return axios.post(UserInfoAsync, {}, {
    params: { uid }
  }).then(res => {
    return res.code === ERR_CODE ? Promise.resolve(res) : Promise.reject(res)
  }).catch(err => {
    return Promise.reject(err)
  })
}

export function GetUserPlayList (uid) {
  return axios.post(PlayListAsync, {uid}).then(res => {
    if (res.code === ERR_CODE) {
      return Promise.resolve(res.playlist)
    } else {
      return Promise.reject(res)
    }
  }).catch(err => {
    return Promise.reject(err)
  })
}

export const LikeListApi = `${API}/likelist`
/**
 * 喜欢音乐列表
 * 调用此接口 , 传入用户 id, 可获取已喜欢音乐 id 列表(id 数组)
 * @param uid {number} 用户 id
 */
export function GetLikeList (uid = null) {
  return axios.post(LikeListApi, {uid}).then(res => {
    return res.code === ERR_CODE ? Promise.resolve(res.ids) : Promise.reject(res)
  }).catch(err => {
    return Promise.reject(err)
  })
}

export const UserAccountApi = `${API}/user/account`
/**
 * 获取账号信息 登录后调用此接口 ,可获取用户账号信息
 */
export function UserAccountAsync () {
  const url = ENV.NODE_ENV === 'production' ? API_TIMESTAMP(UserAccountApi) : UserAccountApi

  return axios.post(url).then(res => {
    return res.code === ERR_CODE ? Promise.resolve(res) : Promise.reject(res)
  }).catch(err => {
    return Promise.reject(err)
  })
}

export const VipGrowthPointApi = `${API}/vip/growthpoint`
/**
 * vip 成长值 登陆后调用此接口 , 可获取当前会员成长值
 */
export function VipGrowthPointAsync () {
  return axios.post(VipGrowthPointApi).then(res => {
    return res.code === ERR_CODE && res.message === 'success' ? Promise.resolve(res.data) : Promise.reject(res)
  }).catch(err => {
    return Promise.reject(err)
  })
}

export const UserRecordApi = `${API}/user/record`
/**
 * 获取用户播放记录 登录后调用此接口 , 传入用户 id, 可获取用户播放记录
 * @param uid {number} 用户 id
 */
export function UserRecordAsync (uid) {
  return axios.get(UserRecordApi, {
    params: { uid, type: 1 }
  }).then(res => {
    console.log(res.weekData)
    return res.code === ERR_CODE && Array.isArray(res.weekData) ? Promise.resolve(res.weekData.slice(0, 20)) : Promise.reject(res)
  }).catch(err => {
    return Promise.reject(err)
  })
}

export const UserLevelApi = `${API}/user/level`
/**
 * 获取用户等级信息
 * 登录后调用此接口 , 可以获取用户等级信息,包含当前登录天数,听歌次数,
 * 下一等级需要的登录天数和听歌次数,当前等级进度,对应 https://music.163.com/#/user/level
 */
export function UserLevelAsync () {
  return axios.post(UserLevelApi).then(res => {
    return res.code === ERR_CODE ? Promise.resolve(res.data) : Promise.reject(res)
  }).catch(err => {
    return Promise.reject(err)
  })
}
